import matplotlib.pyplot as plt
import numpy as np

# 设置支持 Unicode 的字体
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei','DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False

def demonstrate_composite_continuity():
    x = np.linspace(-2, 2, 1000)
    
    # 内层函数
    g = lambda x: x**2 + 1
    
    # 外层函数
    f = lambda u: np.sin(u)
    
    # 复合函数
    fog = lambda x: f(g(x))
    
    # 验证在x=1处的连续性
    x0 = 1
    u0 = g(x0)
    
    print("复合函数连续性验证:")
    print(f"x₀ = {x0}, g(x₀) = {u0:.6f}, f(g(x₀)) = {fog(x0):.6f}")
    print(f"lim(x→{x0}) g(x) = {g(x0):.6f}")
    print(f"lim(u→{u0}) f(u) = {f(u0):.6f}")
    print(f"lim(x→{x0}) f(g(x)) = {fog(x0):.6f}")
    print("由于 f(g(x₀)) = lim(x→x₀) f(g(x))，复合函数在 x₀ 处连续")
    
    # 绘制图像
    plt.figure(figsize=(12, 4))
    
    plt.subplot(1, 3, 1)
    plt.plot(x, g(x), 'b-', linewidth=2)
    plt.axvline(x=x0, color='r', linestyle='--', alpha=0.7)
    plt.axhline(y=u0, color='r', linestyle='--', alpha=0.7)
    plt.scatter([x0], [u0], color='red', s=50)
    plt.title('内层函数: g(x) = x² + 1')
    plt.grid(True)
    
    plt.subplot(1, 3, 2)
    u_values = np.linspace(0.5, 3, 1000)
    plt.plot(u_values, f(u_values), 'g-', linewidth=2)
    plt.axvline(x=u0, color='r', linestyle='--', alpha=0.7)
    plt.axhline(y=f(u0), color='r', linestyle='--', alpha=0.7)
    plt.scatter([u0], [f(u0)], color='red', s=50)
    plt.title('外层函数: f(u) = sin(u)')
    plt.grid(True)
    
    plt.subplot(1, 3, 3)
    plt.plot(x, fog(x), 'purple', linewidth=2)
    plt.axvline(x=x0, color='r', linestyle='--', alpha=0.7)
    plt.axhline(y=fog(x0), color='r', linestyle='--', alpha=0.7)
    plt.scatter([x0], [fog(x0)], color='red', s=50)
    plt.title('复合函数: f(g(x)) = sin(x² + 1)')
    plt.grid(True)
    
    plt.tight_layout()
    plt.show()

demonstrate_composite_continuity()